/*
 * registerFile.c
 *
 *  Created on: Feb 4, 2012
 *      Author: Craig Markham
 *		Class:  TCSS 371
 *		Assignment:
 */

#include "registerFile.h"


/*
 * =======================================================================================
 * RegisterFile implementation
 * =======================================================================================
 */

/**
 * Register File initialize
 * allocates space for 16 register pointers and calls register_initialize for each slot
*/
RegisterFilePtr registerFile_initialize() {
  int i = 0;
  RegisterFilePtr rf;
  rf = (ushort *) malloc(REGISTER_COUNT * sizeof(ushort));
  for(i = 0; i < REGISTER_COUNT; i++){
    register_init(rf + i);
  }
  registerFile_putRegValue(rf, SP, 0xFFFF);
  return rf;
}

/**
 * Register File getByteFrom
 * returns the byte in register reg_num and HOB or LOB indicated by which, sets error code
*/
uchar registerFile_getByteFrom(RegisterFilePtr rf, uchar reg_num, uchar which, int * error){
  uchar byte;
  if(which == 0)
    byte = register_getLowByte(rf + reg_num - 1, error);
  else
    byte = register_getHighByte(rf + reg_num - 1, error);
  return byte;
}

/**
 * Register File putByteTo
 *  puts the byte into the reg_num and HOB or LOB indicated by which. returns error code
*/
int registerFile_putByteTo(RegisterFilePtr rf, uchar reg_num, uchar which, uchar byte){
  int error;
  if(which == 0)
    error = register_putLowByte(rf + reg_num - 1, byte);
  else
    error = register_putHighByte(rf + reg_num - 1, byte);
  return error;
}

/**
 * Register File getRegValue
 *  returns register content; sets error code
*/
ushort registerFile_getRegValue(RegisterFilePtr rf, uchar reg_num, int * error){
  return register_getRegValue(rf + reg_num - 1, * error);
}

/**
 * Register File putRegValue
 *   puts the value into the designated reg_num register returns an error code
*/
int registerFile_putRegValue(RegisterFilePtr rf, uchar reg_num, ushort value){
  return register_putRegValue(rf + reg_num - 1, value);
}
